<?php 
/**
 * eBay Label Printing Program My Account Manage Account Sub Controller 
 * This is the controller for Website User Manage their Account 
 * @package  eBayLabelPrinter
 * @category Controller
 * @author   Norman Dong <norman.dong@hotmail.com>
 * @version  Initial
 * @copyright LeadCasket.com
 * @link     www.leadcasket.com
 * @license  Commercial
 */
define ("TEMP_USER_GROUP", 0);
class Myaccount_ManageaccountController extends Zend_Controller_Action
{

    public function indexAction(){
	
		echo "shomething";	
    
	}
	
	/**
	 * This is Quick Sign Up Action For Free User to have a Free Trial
	 * Radom Password will be provide for the User and Email will be send Afterward
	 * @param int $a  The first parameter
	 * @param string $b - The second one
	 * @return array   for input
	 */
public function quickSignUpAction(){
		
		//return array();	
		$quickSignUpform = new Form_Quicksignup();
		$this->view->form = $quickSignUpform;
		$this->view->errflag = 0;
		
		//if Form posted 
		if($this->getRequest()->isPost()){
			
			$formData = $this->getRequest()->getPost();
			
				if($quickSignUpform->isValid($formData)){
		//create account ,first get form data			
					$userNameSeller  = $quickSignUpform->getValue('username_seller');
					$userEmailSeller = $quickSignUpform->getValue('user_email_seller');
					$senderName = $quickSignUpform->getValue('name_sender'); 
		            $unzipPassword = $quickSignUpform->getValue('unzip_password'); 
		//check if already registered 
					$seller = new Model_DbTable_Seller();
					
					if(!$seller->checkUniqueSellerName($userNameSeller)){
							$this->view->errflag = 1; //errormessage = "User Name has been Taken,please use another one";
							}
					elseif(!$seller->checkUniqueSeller($userNameSeller,$userEmailSeller)){
							$this->view->errflag = 2; //errormessage = 	 "User Name has been Taken,please use another one";				
							}	
		//Two Unique tests passsed,this should be new user 				
					else{   
						   $passWdHandler = new Model_Passwordhandler();
						   //generate one time uniqueID 
						   $uniqueIdSeller =  $passWdHandler->generateUniqueId();
						   $tempPassword = $passWdHandler->generatePassword();
						   $passWordSeller = $passWdHandler->generateHashPassword($tempPassword,$uniqueIdSeller);
						   //
						   unset($passWdHandler);
		//try to add this New Seller				   
						   if(!$seller->addSeller($userNameSeller,$userEmailSeller,$uniqueIdSeller,$passWordSeller,$unzipPassword,"pass word hint",TEMP_USER_GROUP)){
							   $this->view->errflag = 3; //errormessage = 	 "Add User Failed";	
							   }
						   $idSeller = $seller->getSellerId($userNameSeller);
						   
						   $senderName = $quickSignUpform->getValue('name_sender'); 
						   $addrLine1 = $quickSignUpform->getValue('line1_seller_addr');
						   $addrLine2 =  $quickSignUpform->getValue('line2_seller_addr');
						   $addrCity =  $quickSignUpform->getValue('city_seller_addr');
						   $addrState =  $quickSignUpform->getValue('state_seller_addr');
						   $addrCountry =  $quickSignUpform->getValue('country_seller_addr');
						   $addrPostCode =  $quickSignUpform->getValue('postcode_seller_addr');
						   $addrShortLine1 =  $quickSignUpform->getValue('shortform_line1_addr'); 
						   $addrShortLine2 =  $quickSignUpform->getValue('shortform_line2_addr');       
		//after add in get seller ID ,then add the address
						   if($idSeller){
						   		$sellerAddr = new Model_DbTable_Selleraddress();
								
						   		if(!$sellerAddr->addSellerAddress($idSeller,$senderName,$addrLine1,$addrLine2,$addrCity,$addrState,$addrCountry,$addrPostCode,$addrShortLine1,$addrShortLine2)){
									$this->view->errflag = 5; //errormessage = 	 "Add Address Fail";	
								}
								else{
		//Send successfully Email 							
									$successEmail = new Model_Emailhandler();
									$sellerGroup = new Model_DbTable_Sellergroup();
									$userGroupSeller = $sellerGroup->getSellerGroupName(TEMP_USER_GROUP);
									unset($sellerGroup);
									$successEmail->sendSignUpEmail($userNameSeller, $userEmailSeller, $tempPassword, $unzipPassword, $userGroupSeller);
									unset($successEmail);
		//Create Zend Auther Object
									
									
								}	
						   		unset($sellerAddr);
		//After seller address added , send the seller the temp password 

						   		
								}
						   else{
							   	$this->view->errflag = 4; //errormessage = 	 "Seller Id Not Found";		
									}		
							}	
					}

		//if there is no error , try login and redirect to signup done
				$hiddenLogin = new Model_Loginhandler();
				$loginResult = $hiddenLogin->tryLogin($idSeller,$passWordSeller);
				if($loginResult){
					$this->_redirect('myaccount_manageaccount/sign-up-done');
				}
				else{
					$this->view->errflag = 6; //errormessage = "Seller Login Fail";
				}			
				
				
			}
		
		//setup the user type to temp 
		//add a new address 
		//goto My account Home page 			
		}
	public function signUpAction(){
		
		$signUpform = new Form_Signup();
		$this->view->form = $signUpform;
		$this->view->errflag = 0;
		//if Form posted
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			
			if($signUpform->isValid($formData)){
		//create account ,first get form data
				$userNameSeller  = $signUpform->getValue('username_seller');
				$userEmailSeller = $signUpform->getValue('user_email_seller');
				$passwordInputSeller =  $signUpform->getValue('password_seller');
				$unzipPassword = $signUpform->getValue('unzip_password');
				$passwordHint =  $signUpform->getValue('password_hint');
		//check if already registered
				$seller = new Model_DbTable_Seller();
				
				if(!$seller->checkUniqueSellerName($userNameSeller)){
					$this->view->errflag = 1; //errormessage = "User Name has been Taken,please use another one";
				}
				elseif(!$seller->checkUniqueSeller($userNameSeller,$userEmailSeller)){
					$this->view->errflag = 2; //errormessage = 	 "User Name has been Taken,please use another one";
				}
		//Seller is New		
				else{
					
					$passWdHandler = new Model_Passwordhandler();
		//generate one time uniqueID
					$uniqueIdSeller =  $passWdHandler->generateUniqueId();
					$tempPassword = $passwordInputSeller;
					$passWordSeller = $passWdHandler->generateHashPassword($tempPassword,$uniqueIdSeller);
		//
					unset($passWdHandler);	
					
					if(!$seller->addSeller($userNameSeller,$userEmailSeller,$uniqueIdSeller,$passWordSeller,$unzipPassword,$passwordHint,TEMP_USER_GROUP)){
						$this->view->errflag = 3; //errormessage = 	 "Add User Failed";
					}
		//get seller id
					$tempSeller = new Model_DbTable_Seller();
					$idSeller = $tempSeller->getSellerId($userNameSeller);
					unset($tempSeller);
					
		//if there is no error , try login and redirect to signup done
					$hiddenLogin = new Model_Loginhandler();
					$loginResult = $hiddenLogin->tryLogin($idSeller,$passWordSeller);
					if($loginResult){
		//after login , redirect to personal detail 				
						$this->_redirect('myaccount_personaldetail/');
					}
					else{
						$this->view->errflag = 6; //errormessage = "Seller Login Fail";
					}
															
					
					
				}								
				
				
				
			}	
		}
				
			
				
				
		
	}
	
	public function signUpDoneAction(){
		
		//take quick signup and normal signuup 
	}
	
	public function upgradeAccountAction(){
		
	}	
	public function closeAccountAction(){
		
	}
	public function changePasswordAction(){
		
	}
	
			

}
?>